package com.example.fengyungo.service.impl;

import com.example.fengyungo.constant.AdminConstant;
import com.example.fengyungo.dao.AdminDao;
import com.example.fengyungo.domain.pojo.Admin;
import com.example.fengyungo.service.AdminService;
import com.example.fengyungo.util.AdminUtil;
import com.example.fengyungo.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao adminDao;

    /**
     * 测试
     *
     * @return String
     */
    @Override
    public String test() {
        return AdminUtil.getCurrentAdminId();
    }

    /**
     * 添加管理员
     *
     * @param admin 管理员
     * @return Boolea
     */
    @Override
    public Boolean addAdmin(Admin admin) {
        String adminId = 'a' + CommonUtils.initIdByNowTime();
        // id
        admin.setAdminId(adminId);
        // 创建者id
        admin.setAdminCreatorId(AdminUtil.getCurrentAdminId());
        // 使用默认头像
        admin.setAdminAvatar(AdminConstant.DEFAULT_AVATAR);
        // 创建时间
        admin.setAdminCreateTime(CommonUtils.getNowTime());
        // 设置账号状态为可用
        admin.setAdminStatus(AdminConstant.STATUS_ACCESS);
        // 密码加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encodePassword = passwordEncoder.encode(admin.getAdminPassword());
        admin.setAdminPassword(encodePassword);
        return adminDao.addAdmin(admin) > 0;
    }

    /**
     * 修改管理员
     *
     * @param admin 管理员
     * @return Integer
     */
    @Override
    public Integer updateAdmin(Admin admin) {
        return adminDao.updateAdmin(admin);
    }
}
